package Cache;

//import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;//changed to queue in order to be fifo cache
//import java.util.Stack;

public class Cache {
	private HashMap<String,String>cache;
	private Queue<String>QueueKeys;
	int hashSize;
	/**
	 * cache constrctur
	 * it takes the limited size of the cache
	 * @param int n
	 */
	public Cache(int n){
		hashSize=n;
		cache=new HashMap<String,String>();
		QueueKeys= new LinkedList<String>();
	}
	/**
	 * With method  we can insert a key,value to cache
	 * and also rememeber which item to remove from cache 
	 * if the cache is overflow
	 * @param key ,value
	 */
	public void putTo(String key,String value){
	//fifo cache 
	//when it full,it remove the first inserted key,value
		if(cache.size()==hashSize){
			System.out.println("overflow Queue :"+" is lost");
			
			cache.remove(QueueKeys.poll());
		}
		cache.put(key, value);
		QueueKeys.add(key);
		
	}
	/**
	 * returna the value of this key
	 * @param key
	 * @return String value
	 */
	public String getVal(String key){
		return cache.get(key);
	}
	/**
	 * If this cache contains this item
	 * @param searchItem
	 * @return boolean
	 */
	public boolean contains(String searchItem) {
		
		return cache.containsKey(searchItem);
	}

}
